﻿using DotNetty.Transport.Channels;
using System.Threading.Tasks;
namespace Athena.Transport.DotNetty
{
    using Codec;
    using Message;
    public class DotNettyTransportMessageServerSender : DotNettyTransportMessageSender, ITransportMessageSender
    {
        private readonly IChannelHandlerContext _channelHandlerContext;

        public DotNettyTransportMessageServerSender(IRpcMessageEncoder rpcMessageEncoder, IChannelHandlerContext channelHandlerContext) : base(rpcMessageEncoder)
        {
            _channelHandlerContext = channelHandlerContext;
        }

        public async Task SendAsync(RpcTransportMessage rpcTransportMessage)
        {
            var buffer = GetByteBuffer(rpcTransportMessage);
            await _channelHandlerContext.WriteAsync(buffer);
        }

        public async Task SendAndFlushAsync(RpcTransportMessage rpcTransportMessage)
        {
            var buffer = GetByteBuffer(rpcTransportMessage);
            await _channelHandlerContext.WriteAndFlushAsync(buffer);
        }
    }
}
